package com.sy.live.service.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;

import com.sy.live.dao.IBaseDao;
import com.sy.live.dao.impl.BaseDaoImpl;
import com.sy.live.service.IBaseService;

public class BaseServiceImpl implements IBaseService
{
	private static final Logger logger = Logger.getLogger(BaseDaoImpl.class);

	@Resource(name = "baseDao")
	IBaseDao baseDao;

	@Override
	public <T> boolean addEntity(T t)
	{
		boolean flag = false;
		try
		{
			flag = baseDao.addEntity(t);
		} catch (Exception e)
		{
			logger.error("In BaseService , add entity have a error." + e.getMessage());
		}
		return flag;
	}

	@Override
	public <T> boolean updateEntity(T t)
	{
		boolean flag = false;
		try
		{
			flag = baseDao.updateEntity(t);
		} catch (Exception e)
		{
			logger.error("In BaseService, update Entity have a error." + e.getMessage());
		}
		return flag;
	}

	@Override
	public <T> boolean deleteEntity(T t)
	{
		boolean flag = false;
		try
		{
			flag = baseDao.deleteEntity(t);
		} catch (Exception e)
		{
			logger.error("In BaseService, update Entity have a error." + e.getMessage());
		}
		return flag;
	}

	@Override
	public <T> boolean deleteEntityByPk(Class<T> cla, Serializable pk)
	{
		boolean flag = false;
		try
		{
			T t = baseDao.findEntityByPK(cla, pk);
			if (t != null)
				flag = baseDao.deleteEntity(t);
			else
				logger.info("Entity that you was choosed is not exists.");
		} catch (Exception e)
		{
			logger.error("In BaseService, delete Entity have a error." + e.getMessage());
		}
		return flag;
	}

	@Override
	public <T> T findEntityByPK(Class<T> cla, Serializable pk)
	{
		T t = null;
		try
		{
			t = baseDao.findEntityByPK(cla, pk);
		} catch (Exception e)
		{
			logger.error("In BaseService, find Entity By PK have a error." + e.getMessage());
		}
		return t;
	}

	@Override
	public <T> List<T> findEntityByAll(Class<T> cla)
	{
		List<T> entityList = null;
		entityList = baseDao.findAllEntity(cla);
		return entityList;
	}

	// @Override
	// public <T> List<T> findEntityByHql(String hql,Object...params) {
	// List<T> entityList = this.findEntityByHql(hql,params);
	// return entityList;
	// }

}
